package com.shuoyutech.dp.common.framework.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.api.RedissonReactiveClient;
import org.redisson.config.Config;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Spring Cache
 * @author dyb
 * @date 2021/6/1
 */
@Configuration
public class SpringCacheConfigurer {

    /**
     * Spring Cache 集成
     * @param redissonClient
     * @return
     */
    @Bean
    public CacheManager springCacheManager(RedissonClient redissonClient) {
        Map<String, CacheConfig> config = new HashMap<>();
        // create "testMap" cache with ttl = 24 minutes and maxIdleTime = 12 minutes
        config.put("testMap", new CacheConfig(24 * 60 * 1000, 12 * 60 * 1000));
        return new RedissonSpringCacheManager(redissonClient, config);
    }

    /**
     * 流式客户端
     * @param redissonClient
     * @return
     */
    @Bean
    public RedissonReactiveClient reactiveClient(RedissonClient redissonClient) {
        Config config = redissonClient.getConfig();
        return Redisson.createReactive(config);
    }
}
